import { WereWolfRoleActionbar } from "@/werewolf/components/roleActionbar";
import { showRole } from "@/werewolf/func";
import { WereWolfFirstDayPhase } from "@/werewolf/phases/firstDay";
import { WereWolfPhaseView } from "@/werewolf/phaseView";
import { Dimensions } from "@sapi-game/constants";
import { Game } from "@sapi-game/main";
import { ScriptRunner } from "@sapi-game/Runner/scriptRunner";
import { Duration, RandomUtils } from "@sapi-game/utils";

export class ShowRoleCampView extends WereWolfPhaseView<WereWolfFirstDayPhase> {
    override onAttach(): void {
        super.onAttach();
        this.subscribe(
            Game.events.interval,
            () => {
                this.genParticle();
            },
            new Duration(7)
        );
    }

    override async runPhaseView(r: ScriptRunner): Promise<void> {
        this.state.reset(); //重置身份
        this.players.forEach((p) => p.clear("werewolf:next")); //清除下一步
        this.players.playSound("portal.trigger", { volume: 0.2 });
        this.showTitle("§a");
        await r.wait(30);
        this.showTitle("§e");
        await r.wait(40);
        this.showTitle("§c");
        await r.wait(30);
        this.players.title("§l§6你的职业是...");
        await r.wait(60);
        //分配并显示
        this.state.assign();
        showRole(this.players);
        //给裁判下一步
        this.state.giveNextItem();
        //删除自己
        this.state.deleteComponent(ShowRoleCampView);
    }

    genParticle() {
        for (let i = 0; i < 10; i++) {
            const pos = RandomUtils.randomPointInCircle(-201, -181, 5);
            const loc = Dimensions.Overworld.getTopmostBlock({
                x: pos[0],
                z: pos[1],
            });
            if (!loc) continue;
            Dimensions.Overworld.spawnParticle(
                "minecraft:campfire_smoke_particle",
                loc.location
            );
        }
    }

    showTitle(color: string) {
        this.players.title(color + "§l⭐身份正在抽取⭐");
    }
}
